package com.main;
import com.restfb.FacebookClient;
import com.main.EventUsers;

import java.util.ArrayList;
import com.restfb.Connection;
import com.restfb.types.Post;

//Implements methods:
//ArrayList<EventUsers> getAttendees()
//EventUsers getAttendeeByID(String aID)
//EventUsers getAttendeeByName(String aName) 
//int getNumberOfAttendees() 
//
//ArrayList<EventUsers> getInvited()
//EventUsers getInvitedByID(String aID)
//EventUsers getInvitedByName(String aName) 
//int getNumberOfInvited() 
//
//ArrayList<EventUsers> getNoreply()
//EventUsers getNoreplyByID(String aID)
//EventUsers getNoreplyByName(String aName) 
//int getNumberOfNoreply() 
//
//ArrayList<EventUsers> getMaybe()
//EventUsers getMaybeByID(String aID)
//EventUsers getMaybeByName(String aName) 
//int getNumberOfMaybe() 
//
//ArrayList<EventUsers> getDeclined()
//EventUsers getDeclinedByID(String aID)
//EventUsers getDeclinedByName(String aName) 
//int getNumberOfDeclined() 
//
//String toString() 

public class EventUsersList {

	// The list of attendees for event
	private ArrayList<EventUsers> attendees;
	private ArrayList<EventUsers> invited;
	private ArrayList<EventUsers> noreply;
	private ArrayList<EventUsers> maybe;
	private ArrayList<EventUsers> declined;

	/**
	 * @param attendees
	 */
	// Constructor which should initialize the attendees arraylist.
	// Attendee class is used for each element of the array list, because the
	// connection returns the array list of Post type
	public EventUsersList(FacebookClient facebookClient, String u_eID) {

		// Creating attendee list
		String connection_attendee = u_eID + "/attending";
		Connection<Post> u_attendee_connection = facebookClient
				.fetchConnection(connection_attendee, Post.class);
		CreateAttendeeList(u_attendee_connection);

		// Create invited list
		String connection_invited = u_eID + "/invited";
		Connection<Post> u_invited_connection = facebookClient.fetchConnection(
				connection_invited, Post.class);
		CreateInvitedList(u_invited_connection);

		// Create nonreply list
		String connection_noreply = u_eID + "/noreply";
		Connection<Post> u_norely_connection = facebookClient.fetchConnection(
				connection_noreply, Post.class);
		CreateNoreplyList(u_norely_connection);

		// Create maybe list
		String connection_maybe = u_eID + "/maybe";
		Connection<Post> u_maybe_connection = facebookClient.fetchConnection(
				connection_maybe, Post.class);
		CreateMaybeList(u_maybe_connection);

		// Create declined list
		String connection_declined = u_eID + "/declined";
		Connection<Post> u_declined_connection = facebookClient
				.fetchConnection(connection_declined, Post.class);
		CreateDeclinedList(u_declined_connection);

		// this.attendees = new ArrayList<Attendee>();
		// Attendee attendee;
		// // check connection exists
		// if (u_attendee_connection != null) {
		// for (int i = 0; i < u_attendee_connection.getData().size(); i++) {
		// attendee = new Attendee(u_attendee_connection.getData().get(i)
		// .getId(), u_attendee_connection.getData().get(i)
		// .getName());
		// System.out.println(attendee.toString());
		// this.attendees.add(attendee);
		// }
		// System.out.println(attendees.toString());
		// } else {
		// System.out
		// .println("Please, provide correct connection to eventID/attending");
		// }

	}
	/**
	 * Create Methods for creating event users lists
	 */

	// Create Attendee list Method called from Constructor
	public void CreateAttendeeList(Connection<Post> u_attendee_connection) {
		this.attendees = new ArrayList<EventUsers>();
		EventUsers attendee;
		// check connection exists
		if (u_attendee_connection != null) {
			for (int i = 0; i < u_attendee_connection.getData().size(); i++) {
				attendee = new EventUsers(u_attendee_connection.getData()
						.get(i).getId(), u_attendee_connection.getData().get(i)
						.getName());
//				System.out.println(attendee.toString());
				this.attendees.add(attendee);
			}
//			System.out.println(attendees.toString());
		} else {
			System.out
					.println("Please, provide correct connection to eventID/attending");
		}
	}

	// Create Invited list Method called from Constructor
	public void CreateInvitedList(Connection<Post> u_invited_connection) {
		this.invited = new ArrayList<EventUsers>();
		EventUsers eventuser;
		// check connection exists
		if (u_invited_connection != null) {
			for (int i = 0; i < u_invited_connection.getData().size(); i++) {
				eventuser = new EventUsers(u_invited_connection.getData()
						.get(i).getId(), u_invited_connection.getData().get(i)
						.getName());
//				System.out.println(eventuser.toString());
				this.invited.add(eventuser);
			}
//			System.out.println(invited.toString());
		} else {
			System.out
					.println("Please, provide correct connection to eventID/invited");
		}
	}

	// Create Noreply list Method called from Constructor
	public void CreateNoreplyList(Connection<Post> u_norely_connection) {
		this.noreply = new ArrayList<EventUsers>();
		EventUsers eventuser;
		// check connection exists
		if (u_norely_connection != null) {
			for (int i = 0; i < u_norely_connection.getData().size(); i++) {
				eventuser = new EventUsers(u_norely_connection.getData().get(i)
						.getId(), u_norely_connection.getData().get(i)
						.getName());
//				System.out.println(eventuser.toString());
				this.noreply.add(eventuser);
			}
//			System.out.println(noreply.toString());
		} else {
			System.out
					.println("Please, provide correct connection to eventID/noreply");
		}
	}

	// Create Maybe list Method called from Constructor
	public void CreateMaybeList(Connection<Post> u_maybe_connection) {
		this.maybe = new ArrayList<EventUsers>();
		EventUsers eventuser;
		// check connection exists
		if (u_maybe_connection != null) {
			for (int i = 0; i < u_maybe_connection.getData().size(); i++) {
				eventuser = new EventUsers(u_maybe_connection.getData().get(i)
						.getId(), u_maybe_connection.getData().get(i).getName());
//				System.out.println(eventuser.toString());
				this.maybe.add(eventuser);
			}
//			System.out.println(maybe.toString());
		} else {
			System.out
					.println("Please, provide correct connection to eventID/maybe");
		}
	}

	// Create Declined list Method called from Constructor
	public void CreateDeclinedList(Connection<Post> u_declined_connection) {
		this.declined = new ArrayList<EventUsers>();
		EventUsers eventuser;
		// check connection exists
		if (u_declined_connection != null) {
			for (int i = 0; i < u_declined_connection.getData().size(); i++) {
				eventuser = new EventUsers(u_declined_connection.getData()
						.get(i).getId(), u_declined_connection.getData().get(i)
						.getName());
//				System.out.println(eventuser.toString());
				this.declined.add(eventuser);
			}
//			System.out.println(declined.toString());
		} else {
			System.out
					.println("Please, provide correct connection to eventID/declined");
		}
	}
	/**
	 *  Getters for Users Lists
	 */
	// Get method for array list of attendees
	public ArrayList<EventUsers> getAttendees() {
		return attendees;
	}

	public ArrayList<EventUsers> getInvited() {
		return invited;
	}
	public ArrayList<EventUsers> getNoreply() {
		return noreply;
	}
	public ArrayList<EventUsers> getMaybe() {
		return maybe;
	}
	public ArrayList<EventUsers> getDeclined() {
		return declined;
	}
	
	/**
	 *  Getters for Event User by ID
	 */
	
	// Get method: Returns the Attendee by ID
	public EventUsers getAttendeeByID(String aID) {
		if (aID != null) {
			for (EventUsers attednee : attendees) {
				if (aID.equals(attednee.getID())) {
					return attednee;
				} 
			}
		}
		return null;
	}
	
	// Get method: Returns the Invited by ID
	public EventUsers getInvitedByID(String iID) {
		if (iID != null) {
			for (EventUsers u_invited : invited) {
				if (iID.equals(u_invited.getID())) {
					return u_invited;
				} 
			}
		}
		return null;
	}
	// Get method: Returns the Nonreply by ID
	public EventUsers getNoreplyByID(String nID) {
		if (nID != null) {
			for (EventUsers u_nonreply : noreply) {
				if (nID.equals(u_nonreply.getID())) {
					return u_nonreply;
				} 
			}
		}
		return null;
	}
	// Get method: Returns the maybe by ID
	public EventUsers getMaybeByID(String mID) {
		if (mID != null) {
			for (EventUsers u_maybe : maybe) {
				if (mID.equals(u_maybe.getID())) {
					return u_maybe;
				} 
			}
		}
		return null;
	}
	// Get method: Returns the Declined by ID
	public EventUsers getDeclinedByID(String dID) {
		if (dID != null) {
			for (EventUsers u_declined : declined) {
				if (dID.equals(u_declined.getID())) {
					return u_declined;
				} 
			}
		}
		return null;
	}
	/**
	 *  Getters for Event User by Name
	 */
	// Get method: Returns the Attendee by Name
	public EventUsers getAttendeeByName(String aName) {
		if (aName != null) {
			for (EventUsers attednee : attendees) {
				if (aName.equals(attednee.getName())) {
					return attednee;
				} 
			}
		}
		return null;
	}
	// Get method: Returns the Invited by Name
	public EventUsers getInvitedByName(String iName) {
		if (iName!= null) {
			for (EventUsers u_invited : invited) {
				if (iName.equals(u_invited.getName())) {
					return u_invited;
				} 
			}
		}
		return null;
	}
	// Get method: Returns the Nonreply by Name
	public EventUsers getNoreplyByName(String nName) {
		if (nName != null) {
			for (EventUsers u_nonreply : noreply) {
				if (nName.equals(u_nonreply.getName())) {
					return u_nonreply;
				} 
			}
		}
		return null;
	}
	// Get method: Returns the maybe by Name
	public EventUsers getMaybeByName(String mName) {
		if (mName != null) {
			for (EventUsers u_maybe : maybe) {
				if (mName.equals(u_maybe.getName())) {
					return u_maybe;
				} 
			}
		}
		return null;
	}
	// Get method: Returns the Declined by ID
	public EventUsers getDeclinedByName(String dName) {
		if (dName != null) {
			for (EventUsers u_declined : declined) {
				if (dName.equals(u_declined.getName())) {
					return u_declined;
				} 
			}
		}
		return null;
	}
	
	/**
	 *  Number of Event Users
	 */
	// Get number of attended users
	public int NumberOfAttendees() {
		return attendees.size();
	}

	// Get number of invited users
	public int NumberOfInvited() {
		return invited.size();
	}
	// Get number of noreply users
	public int NumberOfNoreply() {
		return noreply.size();
	}
	// Get number of maybe users
	public int NumberOfMaybe() {
		return maybe.size();
	}
	// Get number of declined users
	public int NumberOfDeclined() {
		return declined.size();
	}
	@Override
	public String toString() {
		return "EventUsersList [attendees=" + attendees + ", invited="
				+ invited + ", noreply=" + noreply + ", maybe=" + maybe
				+ ", declined=" + declined + "]";
	}



}
